package com.youkeda.meetingroom.service.impl;

import com.youkeda.meetingroom.dao.dataobject.MrUser;
import com.youkeda.meetingroom.dao.mapper.MrUserMapper;
import com.youkeda.meetingroom.model.Result;
import com.youkeda.meetingroom.model.User;
import com.youkeda.meetingroom.service.CompanyService;
import com.youkeda.meetingroom.service.UserService;
import com.youkeda.meetingroom.util.PwdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * UserServiceImpl的注释
 *
 * @author: Awake
 * @date: 2019-07-03 17:36
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    CompanyService companyService;
    @Resource
    MrUserMapper mrUserMapper;


    /**
     * 用户注册，给出用户注册信息
     * 用户注册信息包括邮箱、明文密码、姓名、公司主键、是否为领导人
     *
     * @param user 用户注册信息
     * @return 成功信息
     */
    @Override
    public Result register(User user) {
        return null;
    }

    /**
     * 用户登录
     * 通过邮箱和明文密码登录
     *
     * @param email 邮箱
     * @param pwd   明文密码
     * @return
     */
    @Override
    public Result login(String email, String pwd) {
        Result result = new Result();
        MrUser mrUser = mrUserMapper.selectUserByEmail(email);

        //加密后的密码
        String enPwb = PwdUtils.md5Password(pwd,mrUser.getSalt());

        if(mrUser.getPwd().equals(enPwb)){
            result.setSuccess(true);
            User user =new User();
            user.setCompanyId(mrUser.getCompanyId());
            user.setEmail(email);
            user.setId(mrUser.getId());
            user.setName(mrUser.getName());
            user.setPosition(mrUser.getPosition());
            result.setData(user);
            return result;
        }
        result.setSuccess(false);
        result.setErrorMessage("密码错误");
        return result;
    }
}
